import express from 'express';
import nodemailer from 'nodemailer';
import cors from 'cors';
const app = express();
// 中间件
app.use(cors());
app.use(express.json());

const toEmail = 'sgq625@126.com';
// 邮件配置
const transporter = nodemailer.createTransport({
	host: 'smtp.163.com',
	port: 465,
	secure: true,
	auth: {
		user: 'sdhbdwxcb@163.com',
		pass: 'QUR8S3BFYb9s6rCM' // 163邮箱授权码
	}
});

// 发送邮件接口
app.post('/send-email', async (req, res) => {
	try {
		const {
			name,
			phone,
			title,
			content
		} = req.body;

		const mailOptions = {
			from: '"联系人" <sdhbdwxcb@163.com>',
			to: toEmail,
			subject: `${title}`,
			text: `姓名: ${name}\n电话: ${phone}\n内容: ${content}`
		};

		await transporter.sendMail(mailOptions);
		res.status(200).json({
			message: '邮件发送成功'
		});
	} catch (error) {
		console.error('邮件发送失败:', error);
		res.status(500).json({
			message: '邮件发送失败'
		});
	}
});

// 启动服务器
const PORT = 3001;
app.listen(PORT, () => {
	console.log(`邮件服务运行在 http://localhost:${PORT}`);
});